package com.mingmen.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.supplier.R;
import com.mingmen.supplier.defineview.GlideRoundTransform;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.label.bean.LabelTestBean;
import com.mingmen.supplier.model.CommodityShopModel;

import java.util.ArrayList;
import java.util.List;


public class CommodityManagerAdapter extends RecyclerView.Adapter<CommodityManagerAdapter.ViewHolder> {
    List<CommodityShopModel.DataBean> mListData = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;

    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<CommodityShopModel.DataBean> myLiveList);
    }


    public CommodityManagerAdapter(Context context, List<CommodityShopModel.DataBean> myLiveList) {
        this.mContext = context;
        this.mListData = myLiveList;

    }

    public void setDataList(List<CommodityShopModel.DataBean> list) {
        mListData = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_commoditymanager, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv_label.setText(mListData.get(position).getCommodityName());
        CommodityShopModel.DataBean dataBean = mListData.get(position);
        holder.checkItem.setChecked(dataBean.isChecked());
        Glide.with(mContext).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.default_pic).error(R.mipmap.default_pic).transform(new GlideRoundTransform(mContext, 5)).into(holder.img_pic);
        holder.commodityName.setText(dataBean.getCommodityName());
        holder.description.setText(dataBean.getDescription());
        holder.spec.setText("规格: " + dataBean.getSpec());

//        holder.text_manager
        if (dataBean.getFlag().equals("0")) {
            holder.text_manager.setText("增加");
            holder.text_manager.setBackground(mContext.getResources().getDrawable(R.drawable.login_blue_normal));
            holder.text_manager.setTextColor(mContext.getResources().getColor(R.color.normal_bluetextcolor));
            holder.text_already.setVisibility(View.GONE);
        } else if (dataBean.getFlag().equals("1")) {
            holder.text_manager.setText("移除");
            holder.text_manager.setBackground(mContext.getResources().getDrawable(R.drawable.login_red_normal));
            holder.text_manager.setTextColor(Color.parseColor("#AF210C"));
            holder.text_already.setVisibility(View.VISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, mListData);
            }
        });
        holder.text_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, mListData);
            }
        });
        holder.checkItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dataBean.setChecked(!dataBean.isChecked());
                holder.checkItem.setChecked(dataBean.isChecked());
                mOnItemClickListener.onItemClickListener(v, position, mListData);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_label;
        LinearLayout content;
        TextView text_manager;

        CheckBox checkItem;
        ImageView img_pic;
        TextView commodityName;
        TextView description;
        TextView spec;
        TextView text_already;

        public ViewHolder(View itemView) {
            super(itemView);

            tv_label = (TextView) itemView.findViewById(R.id.tv_label);
            content = itemView.findViewById(R.id.content);
            text_manager = itemView.findViewById(R.id.text_manager);
            checkItem = itemView.findViewById(R.id.checkItem);
            img_pic = itemView.findViewById(R.id.img_pic);
            commodityName = itemView.findViewById(R.id.commodityName);
            description = itemView.findViewById(R.id.description);
            spec = itemView.findViewById(R.id.spec);
            text_already = itemView.findViewById(R.id.text_already);
        }
    }


}
